/*
 * @Author: wangtao
 * @Date: 2020-06-29 16:32:41
 * @LastEditors: kanglang
 * @LastEditTime: 2020-10-21 17:23:01
 * @Description: file content
 */

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Modal,
  Text,
  TouchableOpacity,
  PixelRatio,
  TextInput,
  Keyboard,
} from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
import {
  mainBgColorLightGray,
  mainBgColorWhite,
  fontColorDeepRed,
  fontColorBlack,
  fontColorLightGray,
  fontColorCoffee,
  screenWidth,
  px2dp,
  screenHeight,
  splitLineColorLightGray,
  fontColorSecDeepGray,
  mainBgColorBlack,
} from '../../../../../common/styles';
import { Button, msg } from '../../../../../common';
import { fetchRemarks } from '../../../../../api/source-release';
import SelectItem from './select-item';

const { Submit } = Button;

export default class SourceRemark extends Component {
  constructor(props) {
    super(props);
    this.state = {
      remarks: null, // 特殊要求选项
      remark: '', // 选择的特殊要求
      keyboardHeight: '', // 键盘高度
    };
  }

  componentDidMount() {
    fetchRemarks().then((res) => {
      if (res.success) {
        this.setState({ remarks: res.data });
      }
    });
    const { remark } = this.props;
    this.setState({ remark });

    // 监听键盘
    this.keyboardDidShowListener = Keyboard.addListener(
      'keyboardWillShow',
      this._keyboardDidShow.bind(this),
    );
    this.keyboardDidHideListener = Keyboard.addListener(
      'keyboardWillHide',
      this._keyboardDidHide.bind(this),
    );
  }

  componentWillUnmount() {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }

  render() {
    const { onChangeModal } = this.props;
    const { remarks, remark } = this.state;
    return (
      <Modal
        animationType='slide'
        transparent
        visible
        onRequestClose={() => {
          onChangeModal({ type: 'sourceRemarkIsShow', val: false });
          // msg.emit('router: back');
        }}
        onShow={() => {}}
      >
        <TouchableOpacity
          style={styles.mask}
          onPress={() => {
            onChangeModal({ type: 'sourceRemarkIsShow', val: false });
          }}
        />
        <View
          style={[
            styles.infoContainer,
            { marginBottom: Number(this.state.keyboardHeight) }
          ]}
        >
          <View style={styles.header}>
            <TouchableOpacity
              style={styles.cancel}
              onPress={() => {
                onChangeModal({ type: 'sourceRemarkIsShow', val: false });
              }}
            >
              <Text style={{ fontSize: px2dp(32), color: fontColorSecDeepGray }}>
                取消
              </Text>
            </TouchableOpacity>
            <Text
              style={{
                fontSize: px2dp(32),
                color: fontColorBlack,
                fontWeight: 'bold',
              }}
            >
              备注
            </Text>
            <Submit
              text='确定'
              boxStyle={{
                width: px2dp(104),
                height: px2dp(60),
                borderRadius: px2dp(8),
                position: 'absolute',
                right: px2dp(28),
              }}
              disabled={false}
              onClick={() => {
                this.submit();
              }}
            />
          </View>
          {/* 备注 */}
          <ScrollView
            style={styles.editContainer}
            showsVerticalScrollIndicator={false}
          >
            <View style={styles.inputContent}>
              <TextInput
                style={[
                  styles.goodsWrapPriceTextInput,
                  { color: fontColorBlack },
                ]}
                placeholder='选填，请输备注'
                placeholderTextColor='#CDCCC9'
                multiline
                maxLength={100}
                defaultValue=''
                value={remark}
                underlineColorAndroid='transparent'
                onChangeText={(text) => {
                  this.onChangeText(text);
                }}
                onBlur={() => {}}
              />
              <View style={styles.textNumWarp}>
                <Text style={styles.textNum}>
                  {remark ? remark.length : 0}
                  /50
                </Text>
              </View>
            </View>

            <View style={styles.packing}>
              <Text
                style={{
                  fontSize: px2dp(28),
                  fontWeight: 'bold',
                  color: mainBgColorBlack,
                }}
              >
                特殊要求
              </Text>
              <View style={styles.packingSelect}>
                {!!remarks &&
                  remarks.map((item) => {
                    return (
                      <SelectItem
                        key={item.id}
                        name={item.name}
                        type='remark'
                        value={item.name}
                        activeVal=''
                        style={{ width: 'auto', paddingHorizontal: 6 }}
                        onChange={(type, value) => this.onChange(type, value)}
                      />
                    );
                  })}
              </View>
            </View>
          </ScrollView>
        </View>
      </Modal>
    );
  }

  _keyboardDidShow(e) {
    this.setState({
      keyboardHeight: e.startCoordinates.height,
    });
  }

  _keyboardDidHide(e) {
    this.setState({
      keyboardHeight: 0,
    });
  }

  // 改变
  onChangeText = (text) => {
    if (text.length > 50) {
      return;
    }
    this.setState({ remark: text });
  };

  // 改变
  onChange = (type, val) => {
    const { remark } = this.state;
    if (remark) {
      val = `${remark}，${val}`;
      if (val.length >= 50) {
        val = val.slice(0, 50);
      }
    } else {
      val = `${val}`;
    }
    this.setState({ [type]: val });
  };

  // 提交
  submit = () => {
    const { onChangeVal, onChangeModal } = this.props;
    const { remark } = this.state;
    onChangeVal({ type: 'supply', val: { remark } });
    onChangeModal({ type: 'sourceRemarkIsShow', val: false });
  };
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: mainBgColorLightGray,
    justifyContent: 'center',
  },
  mask: {
    width: screenWidth,
    height: screenHeight,
    backgroundColor: 'rgba(0,0,0,0.2)',
    position: 'absolute',
    left: 0,
    top: 0,
  },
  infoContainer: {
    width: screenWidth,
    maxHeight: 0.5 * screenHeight,
    backgroundColor: mainBgColorWhite,
    position: 'absolute',
    bottom: 0,
    left: 0,
    borderRadius: px2dp(12),
    overflow: 'hidden',
    zIndex: 10,
  },
  header: {
    width: screenWidth,
    height: px2dp(108),
    justifyContent: 'center',
    alignItems: 'center',
    borderBottomWidth: 1 / PixelRatio.get(),
    borderColor: splitLineColorLightGray,
  },
  cancel: {
    position: 'absolute',
    left: 0,
    top: 0,
    width: 80,
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  packing: {
    marginTop: px2dp(48),
  },
  packingSelect: {
    marginTop: px2dp(24),
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'flex-start',
  },
  selectItem: {
    width: px2dp(218),
    height: px2dp(72),
    backgroundColor: mainBgColorLightGray,
    borderRadius: px2dp(8),
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: px2dp(16),
    marginRight: px2dp(10),
  },
  editContainer: {
    paddingHorizontal: px2dp(32),
    paddingBottom: px2dp(100),
  },
  inputContent: {
    marginTop: px2dp(48),
    paddingHorizontal: 12,
    paddingVertical: 12,
    backgroundColor: mainBgColorLightGray,
    marginVertical: 10,
  },
  goodsWrapPriceTextInput: {
    fontSize: px2dp(32),
    height: px2dp(280),
    width: '100%',
    backgroundColor: mainBgColorLightGray,
    textAlignVertical: 'top',
  },
  textNumWarp: {
    flex: 1,
    justifyContent: 'flex-end',
    flexDirection: 'row',
  },
  textNum: {
    color: 'rgb(153,153,153)',
    fontSize: 14,
  },
});
